package com.example.woke;

import android.app.Activity;
import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.RelativeLayout;
import android.widget.TextView;
import android.widget.Toast;

import com.example.woke1.FriendinfoActivity;
import com.http.HttpMethods;
import com.loopj.android.http.AsyncHttpClient;
import com.loopj.android.http.JsonHttpResponseHandler;
import com.loopj.android.http.PersistentCookieStore;
import com.loopj.android.http.RequestParams;
import com.woke.method.MyApp;
import com.zhongcai.online.R;

import org.apache.http.Header;
import org.json.JSONException;
import org.json.JSONObject;

import java.util.ArrayList;

public class ZhongcaiReciveActivity extends Activity {

	private TextView mTsure;
	private ArrayList<String> list = new ArrayList<String> ();
	private String[] splits;
	private String order, money, userid;
	private String suborder;
	private String subuserid;
	private boolean lskde;
	private TextView mTcancl;
	private TextView mTlaowang;
	private MyApp application;
	private String result;
	private RelativeLayout mLayout;
	private String user_nicename;//�տ���

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate (savedInstanceState);
		setContentView (R.layout.activity_zhongcai_recive);

		application = (MyApp) getApplication ();
		Intent intent = getIntent ();
		result = intent.getStringExtra ("result");
		intview ();

	}

	private void sendtolaowang() {
		if (application.getDatas_load () == null) return;
		AsyncHttpClient ahc = new AsyncHttpClient ();
		PersistentCookieStore myCookieStore = new PersistentCookieStore (ZhongcaiReciveActivity.this);
		ahc.setCookieStore (myCookieStore);
		RequestParams params = new RequestParams ();
		params.put ("g", "api");
		params.put ("m", "order");
		params.put ("a", "getzcsm");
		params.put ("order_id", suborder);
		params.put ("user_id", application.getDatas_load ().getId ());
		ahc.get (HttpMethods.BASEURL, params, new JsonHttpResponseHandler () {

			@Override
			public void onSuccess(int statusCode, Header[] headers,
					JSONObject response) {
				// TODO Auto-generated method stub
				super.onSuccess (statusCode, headers, response);
//				Log.e("response", ""+response);
				try {
					String flag = response.getString ("flag");
					String info = response.getString ("info");
					if (flag.equals ("success")) {
						Toast.makeText (ZhongcaiReciveActivity.this, "付款成功！",
								Toast.LENGTH_SHORT).show ();
						Intent intent = new Intent (ZhongcaiReciveActivity.this, ZcaipayokActivity.class);
						intent.putExtra ("user_nicename", user_nicename);
						intent.putExtra ("money", money);
						startActivity (intent);
						finish ();
					} else {
						Toast.makeText (ZhongcaiReciveActivity.this, "" + info,
								Toast.LENGTH_SHORT).show ();
					}
				} catch (JSONException e) {
					e.printStackTrace ();
				}

			}

			@Override
			public void onStart() {
				// TODO Auto-generated method stub
				super.onStart ();
				// mBar.setVisibility(View.VISIBLE);
			}

			@Override
			public void onFailure(int statusCode, Header[] headers,
					String responseString, Throwable throwable) {
				super.onFailure (statusCode, headers, responseString, throwable);
				Toast.makeText (ZhongcaiReciveActivity.this, "付款失败！",
						Toast.LENGTH_SHORT).show ();
//				Log.e("response", "ʧ��" + responseString);
			}

			@Override
			public void onFinish() {
				super.onFinish ();
				// mBar.setVisibility(View.GONE);
				// Log.e("response", "���");
			}
		});
	}

	@Override
	protected void onActivityResult(int requestCode, int resultCode, Intent data) {
		// TODO Auto-generated method stub
		super.onActivityResult (requestCode, resultCode, data);

		if (requestCode == 700 && resultCode == 600) {
			sendtolaowang ();
		}

	}

	private void intview() {
		mTlaowang = (TextView) findViewById (R.id.avzhongcai_text_laowang);
		mLayout = (RelativeLayout) findViewById (R.id.frame_zcreciver_pro);
		mTcancl = (TextView) findViewById (R.id.avzhongcai_text_cancel);
		mTcancl.setOnClickListener (new OnClickListener () {

			@Override
			public void onClick(View v) {
				finish ();
			}
		});
		findViewById (R.id.avzhongcai_image_back).setOnClickListener (
				new OnClickListener () {

					@Override
					public void onClick(View v) {
						finish ();
					}
				});

//		}

		mTsure = (TextView) findViewById (R.id.avzhongcai_text_ok);
		mTsure.setOnClickListener (new OnClickListener () {

			@Override
			public void onClick(View v) {
				if (application.getDatas_load () == null) return;
				if (null != application.getDatas_load ().getId ()) {
					if (subuserid.equals (application.getDatas_load ().getId ())) {
						Toast.makeText (ZhongcaiReciveActivity.this, "不能向自己支付", Toast.LENGTH_SHORT)
								.show ();
					} else {
						Intent intent1 = new Intent (ZhongcaiReciveActivity.this, CheckpaypswActivity.class);
						intent1.putExtra ("paymoney", money);
						startActivityForResult (intent1, 700);
					}
				} else {
					Intent intent = new Intent (ZhongcaiReciveActivity.this, LoadActivity.class);
					startActivity (intent);
				}
			}
		});

		if (result.length () > 0 && ! result.contains ("ZC16") && ! result.contains ("http")) {
			Intent intent = new Intent (this, FriendinfoActivity.class);
			intent.putExtra ("userid", result);
			intent.putExtra ("closed", 1);
			startActivity (intent);
			finish ();
		} else if (result.substring (result.length () - 4, result.length ())
				.equals ("ZC16") && result.length () > 5) {

			splits = result.split (",");
			order = splits[0];
			money = splits[1];
			userid = splits[2];
			lskde = true;
			suborder = order.substring (0, order.length ());
			subuserid = userid.substring (0, (userid.length ()));
			Log.e ("ordsuborderer", suborder + "   " + money + "  " + subuserid);

			TextView mTmoney = (TextView) findViewById (R.id.avzhongcai_text_money);
			if (lskde == true) {
				mTmoney.setText ("￥" + money);
			}
			getusername ();
		} else {
			Intent intent = new Intent ();
			intent.setAction ("android.intent.action.VIEW");
			Uri content_url = Uri.parse (result);
			intent.setData (content_url);
			startActivity (intent);
			Toast.makeText (ZhongcaiReciveActivity.this, "" + result, Toast.LENGTH_SHORT).show ();
			finish ();
			Log.e ("ds", "ds");
		}

//		getusername();

	}

	private void getusername() {
		AsyncHttpClient ahc = new AsyncHttpClient ();
		PersistentCookieStore myCookieStore = new PersistentCookieStore (ZhongcaiReciveActivity.this);
		ahc.setCookieStore (myCookieStore);
		RequestParams params = new RequestParams ();
		params.put ("g", "api");
		params.put ("m", "order");
		params.put ("a", "getzcsk");
		params.put ("order_id", suborder);
		params.put ("user_id", subuserid);
		ahc.get (HttpMethods.BASEURL, params, new JsonHttpResponseHandler () {

			@Override
			public void onSuccess(int statusCode, Header[] headers,
					JSONObject response) {
				// TODO Auto-generated method stub
				super.onSuccess (statusCode, headers, response);
				Log.e ("sdf 2112", "" + response);
				try {
					String flag = response.getString ("flag");
					String info = response.getString ("info");
					if (flag.equals ("success")) {
//						mLayout.setVisibility(View.GONE);
						//{"data":{"data":{"money":"565.00","user_nicename":"����"}},"time":1450354157,"info":"�����ɹ�","flag":"success"}
						JSONObject jsonObject = response.getJSONObject ("data");
						JSONObject jsonObject2 = jsonObject.getJSONObject ("data");
						user_nicename = jsonObject2.getString ("user_nicename");
						mTlaowang.setText (user_nicename + "向你收取");
//						Toast.makeText(ZhongcaiReciveActivity.this, "����ɹ���",
//								Toast.LENGTH_SHORT).show();
					} else {
						finish ();
						Toast.makeText (ZhongcaiReciveActivity.this, "" + info,
								Toast.LENGTH_SHORT).show ();
					}
				} catch (JSONException e) {
					e.printStackTrace ();
				}

			}

			@Override
			public void onStart() {
				// TODO Auto-generated method stub
				super.onStart ();
				mLayout.setVisibility (View.VISIBLE);
			}

			@Override
			public void onFailure(int statusCode, Header[] headers,
					String responseString, Throwable throwable) {
				super.onFailure (statusCode, headers, responseString, throwable);
				Toast.makeText (ZhongcaiReciveActivity.this, "付款失败！",
						Toast.LENGTH_SHORT).show ();
//				Log.e("response", "ʧ��" + responseString);
			}

			@Override
			public void onFinish() {
				super.onFinish ();
				mLayout.setVisibility (View.GONE);
				// Log.e("response", "���");
			}
		});
	}



}
